import torch.nn as nn
import  torch
class GCABlock(nn.Module):
    def __init__(self, in_channels, groups=4):
        super(GCABlock, self).__init__()
        self.groups = groups
        self.group_channels = in_channels // groups
        self.gca = nn.ModuleList([
            nn.Sequential(
                nn.AdaptiveAvgPool2d(1),
                nn.Conv2d(self.group_channels, self.group_channels // 4, kernel_size=1, stride=1),
                nn.ReLU(),
                nn.Conv2d(self.group_channels // 4, self.group_channels, kernel_size=1, stride=1),
                nn.Sigmoid()
            ) for _ in range(groups)
        ])

    def forward(self, x):
        group_features = torch.chunk(x, self.groups, dim=1)
        out = []
        for i, group in enumerate(group_features):
            attention = self.gca[i](group)
            out.append(group * attention)
        return torch.cat(out, dim=1)
